//
//  AY_DetailHeaderView1.m
//  AiYue
//
//  Created by boWen on 2023/11/14.
//

#import "AY_DetailHeaderView1.h"
#import "AY_BookVpPage.h"
@interface AY_DetailHeaderView1 ()

@property (strong,nonatomic) UIImageView *bookImage;
@property (strong,nonatomic) UILabel *bookName;
//@property (strong,nonatomic) UILabel *authorLabel;
@property (strong,nonatomic) UILabel *authorInfo;
@property (strong,nonatomic) UILabel *numberLabel;
@property (strong,nonatomic) UILabel *typeLabel;

@property (nonatomic, strong) UIImageView * vpBtn;
@property (strong,nonatomic) UIView *scoreView;
@property (strong,nonatomic) UILabel *scoreLabel;
@property (strong,nonatomic) UILabel *score;
@property (strong,nonatomic) UILabel *numLabel;
@property (strong,nonatomic) UILabel *num;
@property (strong,nonatomic) UIImageView *lineView;


@end

@implementation AY_DetailHeaderView1


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        UIView * bg_View = [AYTool createViewWithFrame:CGRectZero andBg:[UIColor whiteColor] andRadius:YNUM(13) andAddSubView:self];
        [bg_View mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(YNUM(12));
            make.top.equalTo(self).offset(YNUM(30));
            make.right.equalTo(self).offset(-YNUM(12));
            make.bottom.equalTo(self);
        }];
        
        
        self.bookImage = [AYTool createImageViewWithFrame:CGRectZero andImage:nil andBg:nil andRadius:YNUM(4) andAddSubView:self];
        [self.bookImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(bg_View).offset(YNUM(14));
            make.top.equalTo(self).offset(YNUM(10));
            make.width.mas_equalTo(YNUM(100));
            make.height.mas_equalTo(YNUM(142));
        }];
        
        self.bookName = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#000005") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(18)] andLines:2 andRadius:0 andAddSubView:self];
        [self.bookName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bookImage.mas_right).offset(YNUM(14));
            make.right.equalTo(bg_View.mas_right).offset(YNUM(-14));
            make.top.equalTo(self.bookImage).offset(YNUM(34));
        }];
        
    
        self.typeLabel = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:THEME_COLOR andBgColor:[THEME_COLOR colorWithAlphaComponent:0.1f] andFont:YFont(12) andLines:1 andRadius:YNUM(4) andAddSubView:bg_View];
        self.typeLabel.textAlignment = NSTextAlignmentCenter;
        [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bookName);
            make.bottom.equalTo(self.bookImage.mas_bottom).offset(YNUM(-2));
            make.width.mas_equalTo(YNUM(64));
            make.height.mas_equalTo(YNUM(24));
        }];
        
        self.authorInfo = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#999999") andBgColor:nil andFont:YFont(13) andLines:1 andRadius:0 andAddSubView:bg_View];
        [self.authorInfo mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bookName);
            make.bottom.equalTo(self.typeLabel.mas_top).offset(YNUM(-8));
            make.height.mas_equalTo(YNUM(20));
        }];
        
        self.vpBtn = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"icon_goread_vp.png") andBg:nil andRadius:YNUM(4) andAddSubView:self];
        self.vpBtn.userInteractionEnabled = YES;
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleVpPage)];
        [self.vpBtn addGestureRecognizer:tap];
        [self.vpBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(bg_View).offset(YNUM(14));
            make.right.equalTo(bg_View).offset(YNUM(-14));
            make.height.mas_equalTo(YNUM(36));
            make.top.equalTo(self.bookImage.mas_bottom).offset(YNUM(16));
        }];
        if ([AYMMKV getBoolForKey:ISSHENHE defaultValue:YES]) {
            self.vpBtn.hidden = YES;
        } else {
            self.vpBtn.hidden = NO;
        }
        [self layoutIfNeeded];
        self.lineView = [AYTool createImageViewWithFrame:CGRectMake(-70, 0, 60, self.vpBtn.mj_h) andImage:YImage(@"AY_detail_line") andBg:nil andRadius:0 andAddSubView:self.vpBtn];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(setTimer) userInfo:nil repeats:YES];
        
        self.scoreView = [AYTool createViewWithFrame:CGRectZero andBg:[UIColor whiteColor] andRadius:YNUM(8) andAddSubView:bg_View];
        [self.scoreView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(bg_View);
            make.right.equalTo(bg_View);
            make.top.equalTo(self.vpBtn.mas_bottom).offset(YNUM(5));
            make.height.mas_equalTo(YNUM(65));
        }];
        
        self.score = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#000002") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(20)] andLines:1 andRadius:0 andAddSubView:self.scoreView];
        [self.score mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.scoreView).offset(YNUM(65));
            make.top.equalTo(self.scoreView).offset(YNUM(10));
        }];
        
        self.scoreLabel = [AYTool createLabelWithFrame:CGRectZero andText:@"状态" andTextColor:COLOR(@"#646D75") andBgColor:nil andFont:YFont(12) andLines:1 andRadius:0 andAddSubView:self.scoreView];
        [self.scoreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.score);
            make.top.equalTo(self.score.mas_bottom).offset(YNUM(3));
        }];
        
        self.num = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#000002") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(20)] andLines:1 andRadius:0 andAddSubView:self.scoreView];
        [self.num mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.scoreView).offset(-YNUM(65));
            make.top.equalTo(self.score);
        }];
        
        self.numLabel = [AYTool createLabelWithFrame:CGRectZero andText:@"字数" andTextColor:COLOR(@"#646D75") andBgColor:nil andFont:YFont(12) andLines:1 andRadius:0 andAddSubView:self.scoreView];
        [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.num);
            make.top.equalTo(self.num.mas_bottom).offset(YNUM(3));
        }];
    }
    return self;
}

- (void)setTimer {
    [UIView animateWithDuration:2 animations:^{
        self.lineView.frame = CGRectMake(self.vpBtn.mj_w, 0, 60, self.vpBtn.mj_h);
    } completion:^(BOOL finished) {
        self.lineView.frame = CGRectMake(-70, 0, 60, self.vpBtn.mj_h);
    }];
}

- (void)setBookModel:(BookModel *)bookModel {
    _bookModel = bookModel;
    [self.bookImage sd_setImageWithURL:[NSURL URLWithString:bookModel.coverImg] placeholderImage:YImage(@"placeholderImage")];
    [self.backImage sd_setImageWithURL:[NSURL URLWithString:bookModel.coverImg] placeholderImage:YImage(@"placeholderImage")];
    self.bookName.text = bookModel.title;
    self.authorInfo.text = bookModel.author;
    self.numberLabel.text = [NSString stringWithFormat:@"%.01f万 · %@",[bookModel.wordNum floatValue]/10000,[self getBookStatus:bookModel.status]];
    self.typeLabel.text = bookModel.categoryName;
    self.score.text = [self getBookStatus:bookModel.status];
    self.num.text = [NSString stringWithFormat:@"%.01f万",[bookModel.wordNum floatValue]/10000];
}

- (NSString *)getBookStatus:(NSString *)status {
    switch (status.intValue) {
        case 1:
            return @"连载中";
            break;
        case 2:
            return @"已完结";
            break;
        case 3:
            return @"停更";
            break;
            
        default:
            return @"连载";
            break;
    }
}

- (void)handleVpPage {
    AY_BookVpPage * vp = [[AY_BookVpPage alloc] init];
    vp.bookId = self.bookModel.bookId;
    [[AYTool viewControllerWith:self].navigationController pushViewController:vp animated:YES];
}

- (void)dealloc {
    [self.timer invalidate];
    self.timer = nil;
}
@end
